/*
 * AS3 MultiTouch Client
 *
 * TouchEventSimulatorProvider - Simulate a Touch Interface with mouse and keyboard  
 *
 * Copyright 2008 Artifacts - Fine Software Development
 * http://www.artifacts.de
 * Author: Michael Markowski (m.markowski@artifacts.de)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
 
/**
 * TouchEventSimulatorProvider - Simulate a Touch Interface with mouse and keyboard
 * 
 * Use any key on the keyboard to simulate a finger, e.g. keys 1-5 or "q", "w", "e", "r", "t"
 * The mouse is bound to the last finger that appeared.
 */ 
package de.artifacts.multitouch
{
	import flash.display.DisplayObjectContainer;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	import mx.core.Application;
	
	public class TouchEventSimulatorProvider implements ITouchEventProvider
	{
		
		public var touchManager : TouchManager; // set via ioc by touchManager
		private var selectedCursor : Cursor;
		private var activeCursors : Dictionary;
		private var mouseX : Number;
		private var mouseY : Number;
		
		public function TouchEventSimulatorProvider(container : DisplayObjectContainer)
		{	
			this.touchManager = touchManager;	
			Application.application.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			Application.application.stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			Application.application.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMovedHandler);
			this.activeCursors = new Dictionary();
		}

		public function setTouchManager(manager : TouchManager) : void
		{
			this.touchManager = manager;	
		}
		
		private function mouseMovedHandler(event : MouseEvent) : void
		{
			this.mouseX = event.stageX;
			this.mouseY = event.stageY;			
			if (selectedCursor!=null)
			{
				selectedCursor.position = new Point(mouseX, mouseY);
			}		
			if (selectedCursor!=null && touchManager!=null)
			{
				touchManager.cursorMoved(selectedCursor);
			}	
		}
		
		private function keyDownHandler(event : KeyboardEvent) : void
		{	
			var key : String = String.fromCharCode(event.charCode);
			if (touchManager!=null)
			{		
				if ((selectedCursor=activeCursors[event.keyCode])==null)
				{
					selectedCursor = touchManager.createCursor(event.keyCode);
					touchManager.cursorDown(mouseX, mouseY, event.keyCode, 0);
					activeCursors[event.keyCode] = selectedCursor;
				}				
			}
		}

		private function keyUpHandler(event : KeyboardEvent) : void
		{	
			if (touchManager!=null)
			{		
				touchManager.cursorUp(mouseX, mouseY, event.keyCode);
				touchManager.removeCursor(event.keyCode);
				selectedCursor = null;
				activeCursors[event.keyCode] = null;								
				delete activeCursors[event.keyCode];				
			}
		}

	}
}